package com.zou.chat.msg;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class Sender {
	/*本机ip*/
	public static String localIP = "127.0.0.1";
	/*服务器ip*/
	public static String serverIP = null;
	/*错误信息*/
	public static String err_msg = "";
	 /*默认发送端口*/
	public static int SendPort = 5555;
	/*默认聊天端口*/
	public static int chatPort = 6666;  
	/*接收信息的socket*/
	/*private static DatagramSocket recDs = null;*/
	/*private static DatagramSocket chatSoc = null;*/
	static{
		/*rm = new Random();*/
		/*setLocalIP();*/
		/*chatPort = generateUDPPort();*/
	}
	public Sender(){
/*		try {
			if(recDs == null){
				rePort = generateUsefulPort();
				recDs = new DatagramSocket(rePort);
			}
		} catch (SocketException e) {
			e.printStackTrace();
		}*/
		//findServer();
	}
	/**
	 * @param msgType 消息类别
	 * @param uname
	 * @param friendIP
	 * @param friendPort
	 * @return 发送是否成功
	 */
	public static boolean sendUDPMsg(int msgType,String uname,String friendIP,int friendPort,String messae){
	    try
        {
            /*从命令行得到要发送的内容，使用UTF-8编码将消息转换为字节*/
            byte[] msg = (msgType+"*"+uname+"*"+messae).getBytes("UTF-8");
            /*得到主机的internet地址*/
            InetAddress address = InetAddress.getByName(friendIP);

            /*用数据和地址初始化一个数据报分组（数据包）*/
            DatagramPacket packet = new DatagramPacket(msg, msg.length, address,
            		friendPort);

            /*创建一个默认的套接字，通过此套接字发送数据包*/
            DatagramSocket dSocket = new DatagramSocket();
            dSocket.send(packet);

            /*发送完毕后关闭套接字*/
            dSocket.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            err_msg = "系统运行异常！";
            return false;
        }
		return true;
	}
}
